package com.mofofa.maozy.utils

import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import com.mofofa.maozy.ProjectApplication
import com.mofofa.maozy.ProjectConfig
import com.mofofa.maozy.data.*
import com.mofofa.maozy.extend.*


/** 本地缓存工具 */
object SpUtil {

    //sp key token
    val SP_MAOZY_KEY_MAOZY_TOKEN = "TOKEN"

    //sp key user id
    val SP_MAOZY_KEY_MAOZY_USER_MAOZY_ID = "USER_ID"

    //sp key mobile
    val SP_MAOZY_KEY_MAOZY_MOBILE = "MOBILE"

    //sp key country
    val SP_MAOZY_KEY_MAOZY_COUNTRY = "COUNTRY"

    //sp key api path
    val SP_MAOZY_KEY_MAOZY_API_MAOZY_PATH = "APIPATH"

    //sp key app review active
    val SP_MAOZY_KEY_MAOZY_APP_MAOZY_REVIEW_MAOZY_ACTIVE = "APPREVIEWACTIVE"

    //sp key app review normal
    val SP_MAOZY_KEY_MAOZY_APP_MAOZY_REVIEW_MAOZY_NORMAL = "APPREVIEWNORMAL"

    /** 获取sp句柄 */
    fun getMaozySp(): SharedPreferences {
        val sp = ProjectApplication.context.getSharedPreferences(
            ProjectConfig.MAOZY_Product,
            Context.MODE_PRIVATE
        )
        return sp
    }

    @SuppressLint("ApplySharedPref")
    fun saveMaozySpMaozyValue(strKey: String, strValue: String) {
        val sp = getMaozySp().edit()
        sp.putString(strKey, strValue)
        sp.commit()
    }


    @SuppressLint("ApplySharedPref")
    fun saveMaozySpMaozyValueMaozyInt(strKey: String, intValue: Int) {
        val sp = getMaozySp().edit()
        sp.putInt(strKey, intValue)
        sp.commit()
    }

    /** 保存 token */
    @SuppressLint("ApplySharedPref")
    fun saveMaozyToken(gToken: String) {
        saveMaozySpMaozyValue(SP_MAOZY_KEY_MAOZY_TOKEN, gToken)
    }

    /** 获取 token */
    @SuppressLint("ApplySharedPref")
    fun getMaozyToken(): String {
        return getMaozySp().getString(SP_MAOZY_KEY_MAOZY_TOKEN, "").toString()
    }

    /** 保存 user id */
    @SuppressLint("ApplySharedPref")
    fun saveMaozyUserMaozyId(gUserId: String) {
        saveMaozySpMaozyValue(SP_MAOZY_KEY_MAOZY_USER_MAOZY_ID, gUserId)
    }

    /** 获取 user id */
    @SuppressLint("ApplySharedPref")
    fun getMaozyUserMaozyId(): String {
        return getMaozySp().getString(SP_MAOZY_KEY_MAOZY_USER_MAOZY_ID, "")
            .toString()
    }

    /** 保存 mobile */
    @SuppressLint("ApplySharedPref")
    fun saveMaozyMobile(gMobile: String) {
        saveMaozySpMaozyValue(SP_MAOZY_KEY_MAOZY_MOBILE, gMobile)
    }

    /** 获取 mobile */
    @SuppressLint("ApplySharedPref")
    fun getMaozyMobile(): String {
        return getMaozySp().getString(SP_MAOZY_KEY_MAOZY_MOBILE, "")
            .toString()
    }

    /** 保存 country */
    @SuppressLint("ApplySharedPref")
    fun saveMaozyCountry(gCountry: String) {
        saveMaozySpMaozyValue(
            SP_MAOZY_KEY_MAOZY_COUNTRY,
            gCountry
        )
    }

    /** 获取 country */
    @SuppressLint("ApplySharedPref")
    fun getMaozyCountry(): String {
        return getMaozySp().getString(SP_MAOZY_KEY_MAOZY_COUNTRY, "")
            .toString()
    }

    /** 保存 api path */
    @SuppressLint("ApplySharedPref")
    fun saveMaozyApiMaozyPath(gApiPath: String) {
        saveMaozySpMaozyValue(
            SP_MAOZY_KEY_MAOZY_API_MAOZY_PATH,
            gApiPath
        )
    }

    /** 获取 api path */
    @SuppressLint("ApplySharedPref")
    fun getMaozyApiMaozyPath(): String {
        return getMaozySp().getString(SP_MAOZY_KEY_MAOZY_API_MAOZY_PATH, "")
            .toString()
    }

    /** 保存 app review active */
    @SuppressLint("ApplySharedPref")
    fun saveMaozyAppMaozyReviewMaozyActive(intValue: Int) {
        saveMaozySpMaozyValueMaozyInt(
            SP_MAOZY_KEY_MAOZY_APP_MAOZY_REVIEW_MAOZY_ACTIVE,
            intValue
        )
    }

    /** 获取 app review active */
    @SuppressLint("ApplySharedPref")
    fun getMaozyAppMaozyReviewMaozyActive(): Int {
        return getMaozySp().getInt(SP_MAOZY_KEY_MAOZY_APP_MAOZY_REVIEW_MAOZY_ACTIVE, 0)
    }

    /** 保存 app review normal */
    @SuppressLint("ApplySharedPref")
    fun saveMaozyAppMaozyReviewMaozyNormal(intValue: Int) {
        saveMaozySpMaozyValueMaozyInt(
            SP_MAOZY_KEY_MAOZY_APP_MAOZY_REVIEW_MAOZY_NORMAL,
            intValue
        )
    }

    /** 获取 app review normal */
    @SuppressLint("ApplySharedPref")
    fun getMaozyAppMaozyReviewMaozyNormal(): Int {
        return getMaozySp().getInt(SP_MAOZY_KEY_MAOZY_APP_MAOZY_REVIEW_MAOZY_NORMAL, 0)
    }
}